﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace eRechnung_3_0
{
    public partial class FrmSkonto : Form
    {
        public FrmSkonto()
        {
            InitializeComponent();
            eRechnung_3_0.Globals.ThisDocument.FillSkontoTable(this.dsInvoice);
            this.lblReBetrag.Text = eRechnung_3_0.Globals.ThisDocument.BetragExklMwSt.ToString("#,##0.0");
            this.lblReDatum.Text = eRechnung_3_0.Globals.ThisDocument.CC_InvoiceDate.Text;
        }

        private void CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            string errorTxt = string.Empty;
            DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
            cell.ErrorText = "";

            switch (dataGridView1.Columns[e.ColumnIndex].Name)
            {
                case "spBisDatum":
                    try
                    {
                        DateTime Datum = DateTime.Parse(e.FormattedValue.ToString());
                        DateTime ReDatum = DateTime.Parse(eRechnung_3_0.Globals.ThisDocument.CC_InvoiceDate.Text);
                        TimeSpan ts = Datum.Subtract(ReDatum);
                        double days = ts.TotalDays;
                        if (days < 0)
                        {
                            errorTxt = dataGridView1.Columns[e.ColumnIndex].HeaderText + " liegt vor dem Rechnungsdatum.";
                            e.Cancel = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        errorTxt = dataGridView1.Columns[e.ColumnIndex].HeaderText + " ist kein Datum.";
                        e.Cancel = true;
                    }
                    break;
                case "spSkontoProz":
                case "spSkontoTage":
                    if (e.FormattedValue.ToString()=="")
                    {
                        return;
                    }
                    try
                    {
                        decimal wert = decimal.Parse(e.FormattedValue.ToString());
                        if (wert < 0)
                        {
                        errorTxt = dataGridView1.Columns[e.ColumnIndex].HeaderText + " ist kleiner als 0.";
                        e.Cancel = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        errorTxt = dataGridView1.Columns[e.ColumnIndex].HeaderText + " ist nicht numerisch.";
                        e.Cancel = true;
                    }

                    break;
                default:
                    break;
            }

            switch (dataGridView1.Columns[e.ColumnIndex].Name)
            {
                case "spSkontoProz":
                    if ((e.FormattedValue != null) && (e.Cancel == false))
                    {
                        if (decimal.Parse(e.FormattedValue.ToString()) > 100)
                        {
                            errorTxt = "Skonto % ist größer 100.";
                            e.Cancel = true;
                        }
                    }
                    break;

                default:
                    break;

            }

            if (e.Cancel == true)
            {
                // MessageBox.Show(errorTxt, "Eingabefehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
                cell.ErrorText = errorTxt;
            }
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            eRechnung_3_0.Globals.ThisDocument.SaveSkonto(this.dsInvoice);
            this.Close();
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            CellValidating(sender, e);
        }


        private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
        {
            e.Row.Cells["spBisDatum"].Value = DateTime.Now;
            e.Row.Cells["spSkontoProz"].Value = 0;
            e.Row.Cells["spSkontoTage"].Value = 0;
            e.Row.Cells["spSkontoBetrag"].Value = 0;
            e.Row.Cells["spBasisBetrag"].Value = eRechnung_3_0.Globals.ThisDocument.BetragExklMwSt;
        }

        private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            // spSkontoTage
            // spBisDatum
            DateTime ReDatum = DateTime.Parse(eRechnung_3_0.Globals.ThisDocument.CC_InvoiceDate.Text);
            decimal tage = 0;
            string wert = null;
            DateTime Datum;
            TimeSpan ts = new TimeSpan() ;
            switch ((dataGridView1.Columns[e.ColumnIndex].Name))
            {
                case "spSkontoTage":
                    // spBisDatum ausrechnen
                    wert = this.dataGridView1.Rows[e.RowIndex].Cells["spSkontoTage"].EditedFormattedValue.ToString();
                    if (wert == "")
                    {
                        return;
                    }
                    tage = decimal.Parse(wert.ToString());
                    ts = new TimeSpan(Convert.ToInt32(tage),0,0,0);
                    Datum=ReDatum.Add(ts);
                    this.dataGridView1.Rows[e.RowIndex].Cells["spBisDatum"].Value = Datum;
                    break;
                case "spBisDatum":
                    // Skontotage ausrechnen
                    wert = this.dataGridView1.Rows[e.RowIndex].Cells["spBisDatum"].EditedFormattedValue.ToString();
                    if (wert == "")
                    {
                        return;
                    }
                    Datum = DateTime.Parse(wert.ToString());
                    ts = Datum.Subtract(ReDatum);
                    tage = Convert.ToDecimal(ts.TotalDays);
                    this.dataGridView1.Rows[e.RowIndex].Cells["spSkontoTage"].Value = tage;
                    break;
                default:
                    break;
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridView1.Columns[e.ColumnIndex].Name == "BtnDel")
            {
                this.dataGridView1.Rows.RemoveAt(e.RowIndex);
            }
        }



    }
}
